﻿/*
 * Copyright (c) 2011 http://DesignBased.NET 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the 
 * Software  * without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 */

using System;
using System.Net;
using System.IO;

namespace T163.Open.Api
{
    internal class OAuthResponse
    {
        HttpWebResponse _response = null;

        internal OAuthResponse(HttpWebResponse response)
            : this(response, 0)
        {

        }

        internal OAuthResponse(HttpWebResponse response, int retryCount)
            : this(response, true, retryCount)
        {
        }

        internal OAuthResponse(HttpWebResponse response, bool isSuccess, int retryCount)
        {
            IsSuccess = isSuccess;

            if (response == null)
            {
                throw new Exception("Response 不能为 NULL");
            }

            _response = response;

            try
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    ResponseText = sr.ReadToEnd();
                }
            }
            catch
            {
                ResponseText = null;
            }

            ContentEncoding = _response.ContentEncoding;
            CharacterSet = _response.CharacterSet;
            ContentLength = _response.ContentLength;
            ContentType = _response.ContentType;
            Cookies = _response.Cookies;
            Headers = _response.Headers;
            IsFromCache = _response.IsFromCache;
            IsMutuallyAuthenticated = _response.IsMutuallyAuthenticated;
            LastModified = _response.LastModified;
            Method = _response.Method;
            ProtocolVersion = _response.ProtocolVersion;
            ResponseUri = _response.ResponseUri;
            Server = _response.Server;
            StatusCode = _response.StatusCode;
            StatusDescription = _response.StatusDescription;
        }

        /// <summary>
        /// 请求是否成功
        /// </summary>
        internal bool IsSuccess
        {
            get;
            private set;
        }

        /// <summary>
        /// 响应返回的内容
        /// </summary>
        internal string ResponseText
        {
            get;
            private set;
        }
                
        /// <summary>
        /// 各应的返回内容的编码
        /// </summary>
        internal string ContentEncoding { get; private set; }

        /// <summary>
        /// 响应的字符集
        /// </summary>
        internal string CharacterSet { get; private set; }

        /// <summary>
        /// 各应的内容的长度
        /// </summary>
        internal long ContentLength { get; private set; }

        /// <summary>
        /// 响应的内容的类型
        /// </summary>
        internal string ContentType { get; private set; }

        /// <summary>
        /// 响应的 cookies
        /// </summary>
        internal CookieCollection Cookies { get; private set; }

        /// <summary>
        /// 响应的 headers
        /// </summary>
        internal WebHeaderCollection Headers { get; private set; }

        /// <summary>
        /// 响应是否是从缓存中读取的
        /// </summary>
        internal bool IsFromCache { get; private set; }

        /// <summary>
        /// 客户端和服务端是否已相互认证
        /// </summary>
        internal bool IsMutuallyAuthenticated { get; private set; }

        /// <summary>
        /// 响应的内容的最后修改时间
        /// </summary>
        internal DateTime LastModified { get; private set; }

        /// <summary>
        /// 响应的 Http 方法
        /// </summary>
        internal string Method { get; private set; }

        /// <summary>
        /// 响应的 Http 协议的版本
        /// </summary>
        internal Version ProtocolVersion { get; private set; }

        /// <summary>
        /// 响应的地址
        /// </summary>
        internal Uri ResponseUri { get; private set; }

        /// <summary>
        /// 响应的服务器名
        /// </summary>
        internal string Server { get; private set; }

        /// <summary>
        /// 响应的状态码
        /// </summary>
        internal HttpStatusCode StatusCode { get; private set; }

        /// <summary>
        /// 响应的状态码的描述
        /// </summary>
        internal string StatusDescription { get; private set; }
    }
}
